<?php
class libfile {
 public function get_upload_file_auth($idx) {
        if(empty($this->_CFG->upload_file_tmp_dir)) {
            throw new debug('unset upload file temp dir');
        }
        $v = sha1(randstr(6,10).time());
        if(!isset($this->session->upload_file_auth)) {
            $a = array($idx=>$v);
            $this->session->upload_file_auth = $a;
        } else {
            $value = $this->session->upload_file_auth;
            $value[$idx] = $v;
            $this->session->upload_file_auth = $value;
        }
    }
    public function destroy_upload_file_auth($idx) {
        unset($this->session->upload_file_auth[$idx]);
    }
    public function check_upload_file_auth($idx) {
        return isset($this->session->upload_file_auth[$idx]);
    }
    public function get_tmp_path() {
        $tmp_dir_first = substr($this->_CFG->upload_file_tmp_dir,0,1);
        if($tmp_dir_first == '/') {
            $tmp_dir = $this->_CFG->upload_file_tmp_dir;
        } else {
            if(substr($this->_CFG->upload_file_tmp_dir,0,2) == './') {
                $tmp_dir = __X_APP_ROOT__ .substr($this->_CFG->upload_file_tmp_dir,1,-1);
            } else {
                $tmp_dir = __X_APP_ROOT__. '/'.$this->_CFG->upload_file_tmp_dir;
            }
        }
        if(substr($tmp_dir,-1,1) != '/') {
            $tmp_dir .= '/';
        }
        $tmp_dir = str_replace('\\','/',$tmp_dir);
        return $tmp_dir;
    }
    public function upfile_tmp($idx) {
        if(!$this->check_upload_file_auth($idx)) return UNAUTH_ACCESS;
        if($_FILES[$idx]['error'] != UPLOAD_ERR_OK) return $_FILES[$idx]['error'];
        if(empty($_FILES[$idx]) || empty($_FILES[$idx]['tmp_name'])) return UPFILE_NOT_EXISTS;
        if($this->check_file_type($_FILES[$idx]['tmp_name']) == false) return UPFILE_FILE_TYPE_ERROR;
        if(($filesize = filesize($_FILES[$idx]['tmp_name'])) > $this->_CFG->upfile_size) {
            @unlink($_FILES[$idx]['tmp_name']);
            return UPFILE_SIZE_LARGER;
        }
        $type = is_image($this->file_mime) ? 'images' : 'file';
        $suffix = ext($this->file_mime);
        $upfle_info = array();
        $upfle_info['src_name'] = $_FILES[$idx]['name'];
        $upfle_info['size'] = byte_format($filesize);
        $upfle_info['tmp_name'] = sha1(randstr(5,9).time().$upfle_info['src_name']) .'.'. $suffix;
        $tmp_dir = $this->get_tmp_path(); 
        $dest = $tmp_dir . $upfle_info['tmp_name'];
        $upfle_info['tmp_path'] = $dest;
        if(@move_uploaded_file($_FILES[$idx]['tmp_name'], $dest)) {
            $this->destroy_upload_file_auth($idx);
            return $upfle_info;
        }
        return UPFILE_FAILURE;
    }
    public function upload_file($idx) {
        if(empty($_FILES[$idx])) {
            return false;
        } else if(empty($_FILES[$idx]['tmp_name'])) {
            throw new debug('缓存文件不存在');
            return false;
        } else if($this->check_file_type($_FILES[$idx]['tmp_name'])){
            $type = is_image($this->file_mime) ? 'images' : 'file';
            $suffix = ext($this->file_mime);
            $dir =  $this->_CFG['upload_dir'] . '/' . $type;
            if(!is_dir(__X_WEB_ROOT__. '/'. $dir)) {
                mkdir(__X_WEB_ROOT__ .'/'. $dir);
            }
            if(filesize($_FILES[$idx]['tmp_name']) > $_ENV['_CFG']['upfile_size']) {
                @unlink($_FILES[$idx]['tmp_name']);
                $this->upfile_error = '上传文件过大';
                return false;
            }
            if(file_exists($dir . '/' . 'LASTEST')) {
                $dir_num = file_get_contents($dir .'/'.'LASTEST');
            } else {
                $dir_num = 0;
                mkdir(__X_WEB_ROOT__ .'/'. $dir . '/' . $dir_num);
                file_put_contents(__X_WEB_ROOT__ . '/' . $dir . '/' . 'LASTEST', '0');
                file_put_contents(__X_WEB_ROOT__ . '/' . $dir . '/' .$dir_num .'/' . 'LASTEST', '0');
            }
            $file_num = file_get_contents($dir. '/' . $dir_num .'/' . 'LASTEST');
            if($file_num >= 3000) {
                $dir_num++;
                mkdir(__X_WEB_ROOT__ . '/'.$dir . '/' . $dir_num);
                file_put_contents(__X_WEB_ROOT__ .'/'.$dir . '/'.$dir_num .'/' . 'LASTEST',0);
            }
            $dir = $dir . '/'. $dir_num;
            $rand_file = md5(randstr(5,10). '/' . $_FILES[$idx]['name']);
            $new_file_name =  '/'.$dir . '/' . $rand_file . '.'.$suffix;
            $dest = __X_WEB_ROOT__ . '/' . $new_file_name;
            if(@move_uploaded_file($_FILES[$idx]['tmp_name'], $dest)) {
                $source_name = conv_quotation($_FILES[$idx]['name']);
                return array('source_name'=>$source_name, 'web_path'=>$new_file_name, 'absolute_path'=> $dest, 'mime'=> $this->file_mime);
            } else {
                $_ENV['debug']['error_type'] = 'upload_file';
                $_ENV['debug']['debug_backtrace'] = debug_backtrace();
                $_ENV['debug']['error_info'] = '移动上传文件错误';
                $this->upfile_error = '移动上传文件错误';
                return false;
            }
        }
    }
}
